package feedback.web.bean.admin;
/**
 * Created by Hien on 5/19/2015.
 */
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.StreamedContent;

import feedback.core.models.entities.Person;
import feedback.core.services.LoginServices;
import feedback.web.util.FacesUtil;
import feedback.web.util.RolerType;

@ManagedBean
@SessionScoped
public class LoginBean {
	/**
	 * 
	 */
	@ManagedProperty("#{loginServices}")
	private LoginServices services;
	private String username;
	private String password;
	private boolean studentIslogin = false;
	private boolean teacherIslogin = false;
	private Person userLogin;
	private StreamedContent content;
	@PostConstruct
	public void init() {
		
	}

	public String login() {
		try{
			Person tmp = services.login(username, password);
			if(tmp == null){
				warn("", "UserID or password is wrong!");
				return null;
			}
			setUserLogin(tmp);
			System.out.println(tmp.getPersonName());
			if(tmp.hasRoler(RolerType.ROL_ADMIN)){
				return "admin";
			}else {
				if(tmp.isTeacher()){
					return "teacher";
				}else{
					return "student";
				}
			}
		
		} catch (Exception e) {
		}
		return null;
	}
	

	public void info(String title, String detail) {
		FacesUtil.getFacesContext().addMessage(null,
				new FacesMessage(FacesMessage.SEVERITY_INFO, title, detail));
	}

	public void warn(String title, String detail) {
		FacesUtil.getFacesContext().addMessage(null,
				new FacesMessage(FacesMessage.SEVERITY_WARN, title, detail));
	}

	public void error(String title, String detail) {
		FacesUtil.getFacesContext().addMessage(null,
				new FacesMessage(FacesMessage.SEVERITY_ERROR, title, detail));
	}

	public void fatal(String title, String detail) {
		FacesUtil.getFacesContext().addMessage(null,
				new FacesMessage(FacesMessage.SEVERITY_FATAL, title, detail));
	}

	public void handleFileUpload(FileUploadEvent event) {
		FacesMessage message = new FacesMessage("Succesful", event.getFile()
				.getFileName() + " is uploaded.");
		FacesContext.getCurrentInstance().addMessage(null, message);
	}
	public String cancel() {
		return null;
	}

	public String logout() {
		setUserLogin(new Person() {
			
			@Override
			public boolean isTeacher() {
				// TODO Auto-generated method stub
				return false;
			}
			
			@Override
			public boolean isStudent() {
				// TODO Auto-generated method stub
				return false;
			}
			
			@Override
			public String is() {
				// TODO Auto-generated method stub
				return null;
			}
		});
		return "welcome";
	}

	

	public LoginServices getServices() {
		return services;
	}

	public void setServices(LoginServices services) {
		this.services = services;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public boolean isStudentIslogin() {
		return studentIslogin;
	}

	public void setStudentIslogin(boolean studentIslogin) {
		this.studentIslogin = studentIslogin;
	}

	public boolean isTeacherIslogin() {
		return teacherIslogin;
	}

	public void setTeacherIslogin(boolean teacherIslogin) {
		this.teacherIslogin = teacherIslogin;
	}

	public StreamedContent getContent() {
		return content;
	}

	public void setContent(StreamedContent content) {
		this.content = content;
	}

	public Person getUserLogin() {
		return userLogin;
	}

	public void setUserLogin(Person userLogin) {
		this.userLogin = userLogin;
	}
	
	
	

}
